#include <iostream>
#include <sqlite3.h>
#include <cstring>
#include <nlohmann/json.hpp>

using namespace std;
using json=nlohmann::json;

const char* sqlite3_filename="times.db";

struct Result_sqlite
{
    int count;
    char** res;
    int col;
    int row;
};

void sqlite3_idu(string sqls)
{
    sqlite3 *db;
    char** res=NULL;
    int row,col;
    char *err=NULL;
    const char* sql;
    sql=sqls.c_str();
    sqlite3_open(sqlite3_filename,&db);

    sqlite3_exec(db,sql,NULL,0,&err);
}

string sqlite3_select(string sqls)
{
    sqlite3 *db;
    char** res=NULL;
    int row,col;
    char *err=NULL;
    const char* sql;
    sql=sqls.c_str();
    sqlite3_open(sqlite3_filename,&db);
    sqlite3_get_table(db,sql,&res,&row,&col,&err);
    Result_sqlite result_result;
    int count=(row*col)+col;
    result_result.count=count;
    result_result.col=col;
    result_result.row=row;
    result_result.res=res;
    int level=0;
    json json_sqlires;
    json json_sqlires2;
    for (int i = 0; i < result_result.count; i++)
    {
        if (i<result_result.col)
        {
            continue;
        }
        json_sqlires[level]=result_result.res[i];
        level++;
    }
    level=0;
    for (int a = 0; a < result_result.row; a++)
    {
        for (int b = 0; b < result_result.col; b++)
        {
            json_sqlires2[a][b]=json_sqlires[level];
            level++;
        }
    }
    string __tojson_sqlires__=json_sqlires2.dump(4);
    return __tojson_sqlires__;
}

// int main(int argc, char const *argv[])
// {
//     string sql="select * from log limit 0,5;";

//     string s=sqlite3_select(sql);
//     cout << s<<endl;


//     sql="update log set status=\"OK\" where id=1;";

//     sqlite3_idu(sql);

//     return 0;
// }